<?php

global $_GPC;
global $_W;
$shopid = check_right(0);
if (!$shopid) {
    message('请先选择需要管理的店铺', $this->createWebUrl('shop'), 'error');
}
$shop = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_group') . ' WHERE weid = \'' . $_W['uniacid'] . '\' and id=\'' . $shopid . '\'');
$operation = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
$cfg = $this->module['config'];
$printType = _getPrintTpye();
if ($operation == 'display') {
    $list = pdo_fetchall('SELECT * FROM ' . tablename('j_hmoney_print') . ' WHERE weid=\'' . $_W['uniacid'] . '\' and groupid=\'' . $shopid . '\' order by pcate asc, id desc ');
} else {
    if ($operation == 'set') {
        $id = $_GPC['id'];
        $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_print') . ' WHERE id = :id ', array(':id' => $id));
        pdo_update('j_hmoney_print', array('isdefault' => 0), array('pcate' => $item['pcate'], 'groupid' => $shopid));
        pdo_update('j_hmoney_print', array('isdefault' => 1), array('id' => $id));
        message('修改完成', $this->createWebUrl('print'), 'success');
    } else {
        if ($operation == 'post') {
            $id = $_GPC['id'];
            if ($id) {
                $item = pdo_fetch('SELECT * FROM ' . tablename('j_hmoney_print') . ' WHERE id = :id ', array(':id' => $id));
            }
            load()->func('file');
            $dir_url = '../attachment/j_hmoney/print/' . $_W['uniacid'] . '/';
            mkdirs($dir_url);
            if (checksubmit('submit')) {
                $data = array('weid' => $_W['uniacid'], 'groupid' => $shopid, 'pcate' => intval($_GPC['pcate']), 'title' => $_GPC['title'], 'pagewidth' => $_GPC['pagewidth'], 'wpcontent' => htmlspecialchars_decode($_GPC['wpcontent']), 'isdefault' => intval($_GPC['isdefault']), 'num' => intval($_GPC['num']) ? intval($_GPC['num']) : 1);
                if (!$id) {
                    pdo_insert('j_hmoney_print', $data);
                    $id = pdo_insertid();
                } else {
                    pdo_update('j_hmoney_print', $data, array('id' => $id));
                }
                if ($_FILES['printdoc']['name']) {
                    if (file_exists($dir_url . $id . '.doc')) {
                        unlink($dir_url . $id . '.doc');
                        error_reporting(0);
                    }
                    $data2['content'] = $id . '.doc';
                    move_uploaded_file($_FILES['printdoc']['tmp_name'], $dir_url . $data2['content']);
                    pdo_update('j_hmoney_print', $data2, array('id' => $id));
                }
                message('修改完成', $this->createWebUrl('print', array('op' => 'post', 'id' => $id)), 'success');
            }
        } else {
            if ($operation != 'update') {
                if ($operation == 'delete') {
                    $id = intval($_GPC['id']);
                    if ($id) {
                        pdo_delete('j_hmoney_print', array('id' => $id));
                    }
                    message('删除成功', $this->createWebUrl('print'), 'success');
                }
            }
        }
    }
}
include $this->template('web/' . $cfg['floder'] . '/print');